<script lang="ts" setup>
import axios from 'axios'
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = useRouter()

const formData = ref({
  name: '',
  type: ''
})

// 提交新增图书
function submit() {
  axios.post("http://localhost:8080/api/addBook", formData.value)
      .then(() => {
        ElMessage.success("添加成功！")
        router.push("/")  // 返回首页
      })
      .catch(error => {
        ElMessage.error("添加失败：" + error.message)
      })
}
</script>

<template>
  <h1>添加新图书</h1>
  <el-form :model="formData" label-width="80px">
    <el-form-item label="name">
      <el-input v-model="formData.name" />
    </el-form-item>
    <el-form-item label="type">
      <el-input v-model="formData.type" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submit">保存</el-button>
      <el-button @click="router.push('/')">取消</el-button>
    </el-form-item>
  </el-form>
</template>